﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Longbow.Security.Cryptography;
using Bootstrap.Security;
using PetaPoco;
using Longbow.Web.Mvc;

namespace Bootstrap.DataAccess.Model
{
    /// <summary>
    /// ibagentstatistic表实体类
    /// </summary>
    //[TableName("ibagentstatistic")]
    public class AgentStatistic
    {

        public string Date { get; set; }//日期
        public string AgentNo { get; set; }//座席
        public string StatisticHour { get; set; }
        public string WorkingTotalTime { get; set; }//工作总时长
        public string WaitTotalTime { get; set; }//等待总时长
        public string WaitCount { get; set; }//等待次数

        public string CallTotalTime { get; set; }//
        public string CallCount { get; set; }//

        public string OutCallTotalTime { get; set; }//外部呼出通话总时长
        public string OutCallCount { get; set; }//外部呼出通话次数
        public string OutterCallTotalTime { get; set; }//外部呼入通话总时长
        public string OutterCallCount { get; set; }//外部呼入通话次数
        public string InCallTotalTime { get; set; }//内部呼入通话总时长
        public string InCallCount { get; set; }//内部呼入通话次数
        public string InnerCallTotalTime { get; set; }// 内部呼出通话总时长
        public string InnerCallCount { get; set; }//内部呼出通话次数
        public string ProcessTotalTime { get; set; }//处理总时长
        public string ProcessCount { get; set; }//处理次数
        public string ParkTotalTime { get; set; }//小休总时长
        public string ParkCount { get; set; } //小休次数
        public string MonitorTotalTime { get; set; }//监控总时长
        public string MonitorCount { get; set; }//监控次数
        public string ConfTotalTime { get; set; }//会议总时长
        public string ConfCount { get; set; }//会议次数
        public string PickupTotalTime { get; set; }//摘机总时长
        public string PickupCount { get; set; }//摘机次数
        public string HoldTotalTime { get; set; }//保持总时长
        public string HoldCount { get; set; }//保持次数
        public string ACWTotalTime { get; set; }//话后处理总时长
        public string ACWCount { get; set; }//话后处理次数


        //查询呼叫统计
        private string _sql = "SELECT left(StatisticDate,10) AS Date, AgentNo AS AgentNo,sum(StatisticHour) AS StatisticHour,"
        + "sum(WorkingTotalTime) AS WorkingTotalTime, sum(WaitTotalTime) AS WaitTotalTime, sum(WaitCount) AS WaitCount,"
        + "sum(CallTotalTime) AS CallTotalTime, sum(CallCount) AS CallCount, sum(ProcessTotalTime) AS ProcessTotalTime,"
        + "sum(ProcessCount) AS ProcessCount, sum(ParkTotalTime) AS ParkTotalTime, sum(ParkCount) AS ParkCount,"
        + "sum(MonitorTotalTime) AS MonitorTotalTime, sum(MonitorCount) AS MonitorCount, sum(ConfTotalTime) AS ConfTotalTime,"
        + " sum(ConfCount) AS ConfCount, sum(PickupTotalTime) AS PickupTotalTime, sum(PickupCount) AS PickupCount,"
        + "sum(OutCallTotalTime) AS OutCallTotalTime, sum(OutCallCount) AS OutCallCount, sum(InCallTotalTime) AS InCallTotalTime,"
        + "sum(InCallCount) AS InCallCount, sum(InnerCallTotalTime) AS InnerCallTotalTime, sum(InnerCallCount) AS InnerCallCount,"
        + "sum(OutterCallTotalTime) AS OutterCallTotalTime, sum(OutterCallCount) AS OutterCallCount, sum(HoldTotalTime) AS HoldTotalTime,"
        + "sum(HoldCount) AS HoldCount, sum(ACWTotalTime) AS ACWTotalTime, sum(ACWCount) AS ACWCount"
        + " FROM ibagentstatistic ";









        /// <summary>
        /// 查询座席统计
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public virtual Page<AgentStatistic> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string AgentNo)
        {
            var sql = new Sql(_sql);
            if (startTime.HasValue) sql.Where("StatisticDate >= @0", startTime.Value);
            if (endTime.HasValue) sql.Where("StatisticDate < @0", endTime.Value.AddDays(1).AddSeconds(-1));
            //if (startTime == null && endTime == null) sql.Where("StatisticDate > @0", DateTime.Today.AddMonths(0 - DictHelper.RetrieveExceptionsLogPeriod()));
            if (!string.IsNullOrEmpty(AgentNo)) sql.Where("AgentNo like '%" + AgentNo + "%' ");//座席号
            sql.GroupBy(" AgentNo, left(StatisticDate,10) ");
            sql.OrderBy($"{po.Sort} {po.Order}");

            return DbManager.Create().Page<AgentStatistic>(po.PageIndex, po.Limit, sql);
        }


    }
}
